package com.kensure.shike.cj.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import co.kensure.exception.BusinessExceptionUtil;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.MapUtils;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.cj.dao.CJBaomingDao;
import com.kensure.shike.cj.model.CJBaoming;
import com.kensure.shike.cj.model.CJGuige;
import com.kensure.shike.order.service.SKFinPayService;
import com.kensure.shike.user.model.SKUser;
import com.kensure.shike.user.service.SKUserService;

/**
 * 抽奖报名服务实现类
 * 
 * @author fankd created on 2019-11-13
 * @since
 */
@Service
public class CJBaomingService extends JSBaseService {

	@Resource
	private CJBaomingDao dao;
	@Resource
	private BaseKeyService baseKeyService;
	@Resource
	private SKUserService sKUserService;
	@Resource
	private CJGuigeService cJGuigeService;
	@Resource
	private SKFinPayService sKFinPayService;

	public CJBaoming selectOne(Long id) {
		return dao.selectOne(id);
	}

	public List<CJBaoming> selectByIds(Collection<Long> ids) {
		return dao.selectByIds(ids);
	}

	public List<CJBaoming> selectAll() {
		return dao.selectAll();
	}

	public List<CJBaoming> selectByWhere(Map<String, Object> parameters) {
		return dao.selectByWhere(parameters);
	}

	public long selectCount() {
		return dao.selectCount();
	}

	public long selectCountByWhere(Map<String, Object> parameters) {
		return dao.selectCountByWhere(parameters);
	}

	public boolean insert(CJBaoming obj) {
		obj.setId(baseKeyService.getKey("cj_baoming"));
		obj.setStatus(0);
		super.beforeInsert(obj);
		return dao.insert(obj);
	}

	public boolean update(CJBaoming obj) {
		return dao.update(obj);
	}

	public boolean updateByMap(Map<String, Object> params) {
		return dao.updateByMap(params);
	}

	public boolean delete(Long id) {
		return dao.delete(id);
	}

	public boolean deleteMulti(Collection<Long> ids) {
		return dao.deleteMulti(ids);
	}

	public boolean deleteByWhere(Map<String, Object> parameters) {
		return dao.deleteByWhere(parameters);
	}

	/**
	 * 查看是否报名过
	 * 
	 * @param user
	 * @param cjid
	 * @return
	 */
	public List<CJBaoming> selectByUser(Long userid, Long cjid) {
		Map<String, Object> parameters = MapUtils.genMap("userid", userid, "cjid", cjid);
		List<CJBaoming> list = selectByWhere(parameters);
		return list;
	}

	/**
	 * 报名人员
	 * 
	 * @return
	 */
	public List<CJBaoming> selectByStatus(Integer status, Long cjid) {
		Map<String, Object> parameters = MapUtils.genMap("status", status, "cjid", cjid);
		List<CJBaoming> list = selectByWhere(parameters);
		return list;
	}

	/**
	 * 进行报名
	 * 
	 * @param user
	 * @param cjid
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public synchronized void add(SKUser user, Long cjid) {
		List<CJBaoming> list = selectByUser(user.getId(), cjid);
		if (CollectionUtils.isNotEmpty(list)) {
			BusinessExceptionUtil.threwException("请不要重复报名");
		}
		if(StringUtils.isBlank(user.getRealname())){
			BusinessExceptionUtil.threwException("请填写姓名");
		}
		if(StringUtils.isBlank(user.getPhone())){
			BusinessExceptionUtil.threwException("请填写电话");
		}
		
		CJBaoming baoming = new CJBaoming();
		baoming.setCjid(cjid);
		baoming.setUserid(user.getId());
		baoming.setName(user.getRealname());
		baoming.setPhone(user.getPhone());
		insert(baoming);
	}

	/**
	 * 中奖数据
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public synchronized void zhongjiang(Long userid, Long guigeid) {
		CJGuige guige = cJGuigeService.selectOne(guigeid);
		List<CJBaoming> list = selectByUser(userid, guige.getCjid());
		CJBaoming cjbaoming = list.get(0);
		cjbaoming.setStatus(9);

		// 赠送商品
		if (guige.getBbid() != null) {
			cjbaoming.setBbid(guige.getBbid());
			cjbaoming.setYanse(guige.getYanse());
			sKFinPayService.zengsong(guige.getBbid(), guige.getYanse(), userid);
		}
		update(cjbaoming);
	}

	/**
	 * 报名人员
	 * 
	 * @return
	 */
	public List<CJBaoming> selectByStatusHt(Integer status, Long cjid, Integer nd) {
		Map<String, Object> parameters = MapUtils.genMap("status", status, "cjid", cjid);
		if (nd != null) {
			parameters.put("nd", nd);
		}else{
			parameters.put("ndIsNull", 1);
		}
		List<CJBaoming> list = selectByWhere(parameters);
		return list;
	}

	/**
	 * 后台进行抽奖
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public synchronized CJBaoming choujiang(Long guigeid) {
		CJGuige guige = cJGuigeService.selectOne(guigeid);

		List<CJBaoming> list = selectByStatusHt(0, guige.getCjid(), guigeid.intValue());
		CJBaoming cjbaoming = null;
		if (CollectionUtils.isNotEmpty(list)) {
			cjbaoming = list.get(0);
		} else {
			list = selectByStatusHt(0, guige.getCjid(),null);
			if (CollectionUtils.isEmpty(list)) {
				BusinessExceptionUtil.threwException("所有人都已经中奖");
			} else {
				Random random = new Random();
				int i = random.nextInt(list.size());
				cjbaoming = list.get(i);
			}
		}
		cjbaoming.setStatus(9);
		// 赠送商品
		if (guige.getBbid() != null) {
			cjbaoming.setBbid(guige.getBbid());
			cjbaoming.setYanse(guige.getYanse());
			sKFinPayService.zengsong(guige.getBbid(), guige.getYanse(), cjbaoming.getUserid());
		}
		update(cjbaoming);
		return cjbaoming;
	}

}
